package com.sun.module_mine.ui.pinlock

import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Circle
import androidx.compose.material.icons.outlined.Circle
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ProvideTextStyle
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.guru.fontawesomecomposelib.FaIcon
import com.guru.fontawesomecomposelib.FaIcons
import com.sun.module_base.ext.click
import com.sun.module_common.config.passWord
import com.sun.module_common.config.pinSize
import com.sun.module_mine.BiometricActivity
import com.sun.module_network.utils.loge

/**
 *@author:sunc
 *@time :2025/8/14 10:07
 *@desc: 描述 锁屏
 */


@Composable
fun PinLockScreen(modifier: Modifier = Modifier) {
    val inputPin = remember { mutableStateListOf<Int>() } //输入密码集合

    val error = remember { mutableStateOf("") }

    val showSuccess = remember { mutableStateOf(false) }

    val context = LocalContext.current

    if (inputPin.size == 4) {
        if (inputPin.joinToString("") == passWord) {
            showSuccess.value = true
            error.value = ""
        } else {
            inputPin.clear()
            error.value = "锁屏密码错误，请重试！"
        }
    }

    Column(
        modifier = modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.primaryContainer),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Spacer(modifier = Modifier.height(20.dp))
        FaIcon(faIcon = FaIcons.UserLock, size = 64.dp, tint = MaterialTheme.colorScheme.onPrimary)

        Text(
            text = "解锁屏幕",
            style = MaterialTheme.typography.labelMedium,
            modifier = Modifier.padding(16.dp),
            color = MaterialTheme.colorScheme.onPrimary
        )
        Spacer(modifier = Modifier.height(10.dp))
        if (showSuccess.value) {
            Text("屏幕已解锁", style = MaterialTheme.typography.headlineLarge)
        } else {
            Row {
                (0 until pinSize).forEach {
                    Icon(
                        imageVector = if (inputPin.size > it) Icons.Default.Circle else Icons.Outlined.Circle,
                        contentDescription = null,
                        modifier = Modifier.padding(8.dp),
                        tint = MaterialTheme.colorScheme.onPrimary
                    )
                }
            }
            Text(
                text = error.value,
                color = MaterialTheme.colorScheme.error,
                modifier = Modifier.padding(16.dp)
            )
            Spacer(modifier = Modifier.height(10.dp))
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                (1..3).forEach {
                    PinKeyItem(onClick = { inputPin.add(it) }) {
                        Text(
                            text = it.toString(),
                            style = MaterialTheme.typography.bodyMedium,
                            color = MaterialTheme.colorScheme.onSecondary,
                        )
                    }
                }
            }
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                (4..6).forEach {
                    PinKeyItem(onClick = { inputPin.add(it) }) {
                        Text(
                            text = it.toString(),
                            style = MaterialTheme.typography.bodyMedium,
                            color = MaterialTheme.colorScheme.onSecondary
                        )

                    }
                }
            }

            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                (7..9).forEach {
                    PinKeyItem(onClick = { inputPin.add(it) }) {
                        Text(
                            text = it.toString(),
                            style = MaterialTheme.typography.bodyMedium,
                            color = MaterialTheme.colorScheme.onSecondary
                        )
                    }
                }
            }

            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(start = 10.dp),
                horizontalArrangement = Arrangement.SpaceEvenly,
                verticalAlignment = Alignment.CenterVertically
            ) {
                FaIcon(
                    faIcon = FaIcons.Fingerprint,
                    size = 36.dp,
                    tint = MaterialTheme.colorScheme.onPrimary,
                    modifier = Modifier.click {
                        context.startActivity(BiometricActivity.instance(context))
                    })
                PinKeyItem(
                    onClick = { inputPin.add(0) },
                    modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)
                ) {
                    Text(
                        text = "0",
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onSecondary,
                        modifier = Modifier.padding(4.dp)
                    )
                }
                FaIcon(
                    faIcon = FaIcons.Backspace,
                    size = 36.dp,
                    tint = MaterialTheme.colorScheme.onPrimary,
                    modifier =
                        Modifier.clickable {
                            if (inputPin.isNotEmpty()) {
                                inputPin.removeAt(inputPin.lastIndex)
                            }
                        }
                )
            }

        }
    }
}

@Composable
fun PinKeyItem(
    modifier: Modifier = Modifier.padding(8.dp),
    onClick: () -> Unit,
    shape: Shape = MaterialTheme.shapes.small.copy(CornerSize(percent = 50)),
    backgroundColor: Color = MaterialTheme.colorScheme.onPrimary,
    contentColor: Color = MaterialTheme.colorScheme.onPrimary,
    elevation: Dp = 4.dp,
    content: @Composable () -> Unit
) {

    var isPressed by remember { mutableStateOf(false) }
    val scale by animateFloatAsState(if (isPressed) 0.95f else 1f)
    val elevation by animateDpAsState(if (isPressed) 4.dp else 8.dp)
    Surface(
        modifier = modifier
            .clip(shape = shape)
            .graphicsLayer { scaleX = scale;scaleY = scale }
            .shadow(elevation = elevation)
//            .clickable(
//                interactionSource = MutableInteractionSource(),
//                indication = null
//            ) { onClick.invoke() }
            .pointerInput(Unit) {
                detectTapGestures(
                    onPress = {
                        isPressed = true
                        "onPress".loge()
                    },
                    onTap = {
                        isPressed = false
                        onClick.invoke()
                        "onTap".loge()
                    }
                )
            },
        shape = shape,
        color = backgroundColor,
        contentColor = contentColor,
        tonalElevation = elevation
    ) {
        CompositionLocalProvider(LocalContentColor provides contentColor) {
            ProvideTextStyle(MaterialTheme.typography.displayMedium) {
                Box(
                    modifier = Modifier.defaultMinSize(minWidth = 64.dp, minHeight = 64.dp),
                    contentAlignment = Alignment.Center
                ) {
                    content.invoke()
                }
            }
        }
    }
}

